#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Bootloader_TransferData_0x36_test.py
@Time    :   2024/09/11 10:35:23
@Author  :   yangliang
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from numpy import byte, bytes_, int16
from pytest_assume.plugin import assume

import pytest
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

SID_TransferData_0x36_NRC = [   ([0x36],[0x7F,0x36,0x73]),
                                ([0x36,0x03],[0x7F,0x36,0x73]),
                                ([0x36,0x08],[0x7F,0x36,0x73])
                            ] 

SID_TransferData_0x36_NRC_Fun = [   ([0x36], []),
                                    ([0x36, 0x03], []),
                                    ([0x36, 0x08], [])
                                ]


class Test_TransferData_Phy(object):
    @allure.severity('minor')
    @allure.description('Test NRC24 of 0x36 in programming session in bootloader with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    def test_TransferData_NRC24_Phy(self, Driver, action):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x36, 0x01], [0x7F, 0x36, 0x24], 2, iStep, True)

    # @allure.severity('minor')
    # @allure.description('Test NRC92 of 0x36 in programming session in bootloader with physical addressing')
    # @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    # def test_TransferData_NRC92_Phy(self, Driver, action):
    #     iStep = 1
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E, 0x00], [0x7E, 0x00], 2, iStep, True)
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x34, 0x00, 0x44, 0x00, 0x7A, 0x00, 0x00, 0x00, 0x00, 0x28, 0x0A], [0x74, 0x20, 0x04, 0x02], 0, iStep, False)
    #     Driver['power'].set_vltage(18.5)
    #     time.sleep(1)
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '36015A5A5A5A6145EFAFDD13A85F4AD0ECA244FA5496CDD5887F36A012EB8AAF1D566ACB30998A9864A7D7415194F8526AC9D2809D16FAC8CF62FA57737EF730A0C423142EDEE51CE38F6169636D79CD1BD4A274118C1110ECB1841F2037DAA07CCC778976BD1447DECEA54A17A8E0874710BB9359E1CF1EB9526A3D2B', [0x7F, 0x36, 0x92], 0, iStep, False)
    #     Driver['power'].set_vltage(12)

    # @allure.severity('minor')
    # @allure.description('Test NRC93 of 0x36 in programming session in bootloader with physical addressing')
    # @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    # def test_TransferData_NRC93_Phy(self, Driver, action):
    #     iStep = 1
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E, 0x00], [0x7E, 0x00], 2, iStep, True)
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x34, 0x00, 0x44, 0x00, 0x7A, 0x00, 0x00, 0x00, 0x00, 0x28, 0x0A], [0x74, 0x20, 0x04, 0x02], 0, iStep, False)
    #     Driver['power'].set_vltage(6.5)
    #     time.sleep(1)
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '36015A5A5A5A6145EFAFDD13A85F4AD0ECA244FA5496CDD5887F36A012EB8AAF1D566ACB30998A9864A7D7415194F8526AC9D2809D16FAC8CF62FA57737EF730A0C423142EDEE51CE38F6169636D79CD1BD4A274118C1110ECB1841F2037DAA07CCC778976BD1447DECEA54A17A8E0874710BB9359E1CF1EB9526A3D2B', [0x7F, 0x36, 0x92], 0, iStep, False)
    #     Driver['power'].set_vltage(12)

    ids = ["Request {2}".format(req, resp, binascii.b2a_hex(bytearray(req), ' ')) for req, resp in SID_TransferData_0x36_NRC]
    @allure.severity('minor')
    @allure.description('Test NRC73 of 0x36 with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    @pytest.mark.parametrize('req,resp', SID_TransferData_0x36_NRC, ids=ids)
    def test_TransferData_NRC73_Phy(self, Driver, action, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E, 0x00], [0x7E, 0x00], 2, iStep, True)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x34, 0x00, 0x44, 0x00, 0x7A, 0x00, 0x00, 0x00, 0x00, 0x28, 0x0A], [0x74, 0x20, 0x04, 0x02], 0, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)


    @allure.severity('minor')
    @allure.description('Test NRC33 of 0x36 in programming session in bootloader with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    def test_TransferData_NRC33_Phy(self,Driver,action):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x36,0x01], [0x7F,0x36,0x33], 2, iStep, False)

    
    @allure.severity('minor')
    @allure.description('Test NRC7F of 0x36 with physical addressing in default session')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    def test_TransferData_NRC7F_Default_Phy(self,Driver,action):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 1, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x36,0x01], [0x7F,0x36,0x7F], 0, iStep, False)

    @allure.severity('minor')
    @allure.description('Test NRC7F of 0x36 with physical addressing in extended session')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    def test_TransferData_NRC7F_Extended_Phy(self,Driver,action):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 3, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x36,0x01], [0x7F,0x36,0x7F], 0, iStep, False)

    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in SID_TransferData_0x36_NRC_Fun]
    @allure.severity('minor')
    @allure.description('Test NRC93 of 0x36 in programming session in bootloader with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    @pytest.mark.parametrize('req,resp',SID_TransferData_0x36_NRC_Fun,ids = ids)
    def test_TransferData_NRC_No_Resp_Fun(self,Driver,action,req,resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E,0x00], [0x7E,0x00], 2, iStep, True)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False,'UDSFun')

    @allure.severity('minor')
    @allure.description('Test NRC33 of 0x36 in programming session in bootloader with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    def test_TransferData_NRC13_Phy(self,Driver,action):
        iStep = 1
        req = [0x36, 0x01]
        for i in range(1028):
            req.append(0x00)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [0x7F,0x36,0x13], 2, iStep, False)
